package org.compiere.apps.search;

import java.awt.Frame;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.adempiere.plaf.AdempierePLAF;
import org.compiere.apps.ALayout;
import org.compiere.grid.ed.VLookup;
import org.compiere.minigrid.IDColumn;
import org.compiere.swing.CLabel;
import org.compiere.swing.CTextField;
import org.compiere.util.Env;
import org.compiere.util.Msg;

public class InfoCharge extends Info {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2579058772692699126L;
	private int docTypeID;
	
	protected InfoCharge(Frame frame, boolean modal, int WindowNo, String value,boolean multiSelection, String whereClause)
	{

		super (frame, modal, WindowNo, "ch", "C_Charge_ID", multiSelection, whereClause);
		docTypeID = Env.getContextAsInt(Env.getCtx(), WindowNo, "C_DocType_ID");
		log.info("InfoCharge");
		setTitle(Msg.getMsg(Env.getCtx(), "InfoCharge"));
		try
		{
			statInit();
			p_loadedOK = initInfo ();
		}
		catch (Exception e)
		{
			return;
		}
		int no = p_table.getRowCount();
		setStatusLine(Integer.toString(no) + " " + Msg.getMsg(Env.getCtx(), "SearchRows_EnterQuery"), false);
		setStatusDB(Integer.toString(no));
		if (value != null && value.length() > 0)
		{
			fName.setValue(value);
			executeQuery();
		}
		//
		pack();
		//	Focus
		fName.requestFocus();	
	}//  InfoCharge
	
	//  Static Info
	private CLabel lFi_code = new CLabel(Msg.translate(Env.getCtx(), "FI_Code"));
	private CTextField fFi_code =new CTextField(10);
	private CLabel lName = new CLabel(Msg.translate(Env.getCtx(), "Name"));
	private CTextField fName = new CTextField(10);
	
	private static final Info_Column[] c_chargeLayout = {
	
		//C_Charge_ID field
		new Info_Column(Msg.translate(Env.getCtx(), "C_Charge_ID"),
				"ch.C_Charge_ID", IDColumn.class),
		//1-field for charge code		
		new Info_Column(Msg.translate(Env.getCtx(),"FI_Code"),
						"ch.fi_Code",String.class),
		//2-field for charge name
		new Info_Column(Msg.translate(Env.getCtx(), "Name"),
								"ch.Name", String.class),
		//3-field for charge type name
		new Info_Column(Msg.translate(Env.getCtx(), "Group"),
						"ct.Name", String.class),
		new Info_Column(Msg.translate(Env.getCtx(), "Account"),
								"clm.value||'-'||clm.name as acct", String.class),
		new Info_Column(Msg.translate(Env.getCtx(), "IsActive"),
					"ch.IsActive", Boolean.class)
	};
	
	/**
	 *	Static Setup - add fields to parameterPanel
	 *  @throws Exception if Lookups cannot be created
	 */
	private void statInit() throws Exception{
		
		lFi_code.setLabelFor(lFi_code);
		fFi_code.setBackground(AdempierePLAF.getInfoBackground());
		fFi_code.addActionListener(this);
		
		lName.setLabelFor(fName);
		fName.setBackground(AdempierePLAF.getInfoBackground());
		fName.addActionListener(this);
		//
		parameterPanel.setLayout(new ALayout(2,2,true));
		parameterPanel.setLayout(new ALayout(2,2,true));
		//
		parameterPanel.add(lFi_code,null);
		parameterPanel.add(fFi_code,null);
		
		parameterPanel.add(lName, null);
		parameterPanel.add(fName, null);
	}
	 
	/**
	 *	General Init
	 *	@return true, if success
	 */
	private boolean initInfo (){
		
	//  prepare table
		
			StringBuffer where = new StringBuffer("ch.IsActive='Y' and ch.IsSummary='N' ");
			if(docTypeID != 0)
				where.append(" and ch.c_chargetype_id IN(select c_chargetype_id from c_chargetype_doctype where c_doctype_id in("+docTypeID+"))");
			
			prepareTable (c_chargeLayout,
				"(C_Charge ch " +
				"left join c_chargetype ct on ct.c_chargetype_id=ch.c_chargetype_id " +
				"left join c_elementvalue clm on(clm.c_elementvalue_id=ch.c_elementvalue_id )) ",
				where.toString(),
				" ch.fi_Code");
		return true;
	}
	

	@Override
	protected String getSQLWhere() {
		
		StringBuffer sql = new StringBuffer();
		if (fName.getText().length() > 0)
			sql.append(" AND UPPER(ch.Name) LIKE ?");
		if (fFi_code.getText().length() > 0)
			sql.append(" AND UPPER(ch.fi_code) LIKE ?");
		
		return sql.toString();
	}

	@Override
	protected void setParameters(PreparedStatement pstmt, boolean forCount)
			throws SQLException {
		
		int index = 1;
		if (fName.getText().length() > 0)
			pstmt.setString(index++, getSQLText(fName));
		if (fFi_code.getText().length() > 0)
			pstmt.setString(index++, getSQLText(fFi_code));
		
	}
	
	/**
	 *  Get SQL WHERE parameter
	 *  @param f field
	 *  @return Upper case text with % at the end
	 */
	private String getSQLText (CTextField f)
	{
		String s = f.getText().toUpperCase();
		if (!s.endsWith("%"))
			s = "%"+s+"%";
		log.fine( "String=" + s);
		return s;
	} 


}
